$(function () {
    //菜单点击
    $(".J_menuItem").on('click', function () {
        var url = $(this).attr('href');
        $("#J_iframe").attr('src', url);
        return false;
    });
    getJurisdiction();
    //获取个人信息
    getOneAdmin();
    setItemTitle();
});

//设置动态栏目title
function setItemTitle() {
    $.ajax({
        url: urlcore + "/api/indexItem/getAllItemsAdmin",
        type: "GET",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            if (data.success == true) {
                var zz = [];
                zz = data.data;
                var itemtitle="";
                for (var zzz of zz) {
                    if (zzz.itemNum == 1) {
                        itemtitle="itemY1";
                    }else if(zzz.itemNum == 2){
                        itemtitle="itemY2";
                    }else if(zzz.itemNum == 3){
                        itemtitle="itemY3";
                    }else if(zzz.itemNum == 4){
                        itemtitle="itemP1";
                    }else if(zzz.itemNum == 5){
                        itemtitle="itemP2";
                    }else if(zzz.itemNum == 6){
                        itemtitle="itemP3";
                    }else if(zzz.itemNum == 7){
                        itemtitle="itemM3";
                    }else if(zzz.itemNum == 8){
                        itemtitle="itemP4";
                    }

                    assignSpanText(itemtitle,zzz.title);
                }
            } else {
                alert(data.msg);
            }

        }
    });
}

//span text赋值
function assignSpanText(domId, value) {
    $('.' + domId).text(value);
}

/**
 * 退出登录
 */
function loginout() {
    $.ajax({
        url: urlcore + "/api/admin/logout",
        type: "GET",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            if (data.success == true) {
                window.location.href = "login.html";
            } else {
                alert(data.msg);
            }

        }
    });

}

//获取个人信息
function getOneAdmin() {

    $.ajax({
        url: urlcore + "/api/admin/getadmin",
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            if (data.success == true) {
                var loginName = data.data.loginName;
                var name = data.data.name;
                $('#admin_img_user_name').append(
                    '<img alt="image" class="img-circle" src="img/toxiang.jpeg" style="height: 100%; width: 100%;">' +
                    '<h4><a href="javascript:;"  data-toggle="modal" data-target="#updatePassword">' + name + '</a></h4>'
                );
                $('#nick_').text(name);
                $('#adminId').val(data.data.id);
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }

            } else if (data.code == 'PARAMETER_INVALID') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            }
            else {
                alert(data.msg);
            }

        }
    });
}

//修改信息
function toUpdatePassword() {
    var id = $('#adminId').val();
    var oldPassword = $('#oldPassword').val().trim();
    var password = $('#password').val().trim();
    var aginPassword = $('#aginPassword').val().trim();
    var userName = $('#userName').val().trim();
    var nick = $('#nick').val().trim();
    if (password != aginPassword) {
        alert("密码不一致");
        return;
    }
    if (oldPassword == '' && password == '' && aginPassword == '' && userName == '' && nick == '') {
        return;
    }
    $.ajax({
        url: urlcore + "/admin/update/password/old?id=" + id + "&password=" + password + "&oldPassword=" + oldPassword + "&userName=" + userName + "&nick=" + nick,
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            if (data.success == true) {
                alert("修改成功，请重新登陆！");
                top.window.location.href = "login.html";
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }

            } else {
                if (data.msg != '空数据') {
                    alert(data.msg)
                }
            }

        },
        error: function () {

        }
    });
}


function getJurisdiction() {
    $.ajax({
        url: urlcore + "/api/admin/getJurisdiction",
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            if (data.success == true) {
                var list = data.data.parentMenu;
                var list2 = data.data.menu;
                $.each(list, function (i, n) {
                    $("#menu_" + n).css("display", "block");
                });
                $.each(list2, function (i, n) {
                    $("#menu_" + n.id).attr("href", n.url);
                });
            } else {
                window.location.href = "login.html";
            }

        }
    });
}